package com.ficus.road.maintain.strategy.allfactory;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.zh.BridgeDiseaseZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.google.common.base.Objects;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ficus.road.maintain.core.model.HitStatusEnum.NEW;

/**
 * @author xiawen yang
 * @date 2022/5/20 下午4:37
 */
public class BridgeInspectionStrategy implements AllFactoryStrategy<BridgeDiseaseZH> {
    private static final String type = AllFactoryTypeEnum.BRIDGE_INSPECTION.getType();
    private final HitAllFactoryDao hitAllFactoryDao;

    public BridgeInspectionStrategy(HitAllFactoryDao hitAllFactoryDao) {
        this.hitAllFactoryDao = hitAllFactoryDao;
    }

    @Override
    public Hit convert2AllFactoryHit(BridgeDiseaseZH zh) {
        String hitId = zh.getUnid() + "";
        Hit fetchHit = hitAllFactoryDao.fetch(hitId);
        Date createDate = zh.getBodycreatedate();
        Date processDate = zh.getProcessingtime();
        String pictureUrl = zh.getDamagepicture();
        String componentDesc = ObjectUtils.isEmpty(zh.getComponentdesc()) ? "" : zh.getComponentdesc();
        String defeatType = ObjectUtils.isEmpty(zh.getDefeattype()) ? "" : zh.getDefeattype();
        String measure = ObjectUtils.isEmpty(zh.getSectionmeasure()) ? "" : ("采取措施: " + zh.getSectionmeasure());
        if (StringUtils.isNotEmpty(pictureUrl) && pictureUrl.contains(",")) {
            pictureUrl = pictureUrl.split(",")[0];
        }

        String[] coordinates = null;
        GpsLocation gpsLocation = GpsLocation.builder().latitude(31.235870d).longitude(121.621148d).build();
        if (ObjectUtils.isNotEmpty(zh.getXy())) {
            coordinates = zh.getXy().split(",");
        }
        if (ObjectUtils.isNotEmpty(coordinates) && coordinates.length == 2) {
            gpsLocation = GpsLocation.builder()
                    .longitude(Double.parseDouble(coordinates[0].trim()))
                    .latitude(Double.parseDouble(coordinates[1].trim())).build();
        }
        if (ObjectUtils.isEmpty(fetchHit)) {
            Hit hit = new Hit();
            HitAttrs hitAttrs = new HitAttrs();
            hitAttrs.setConfirmation(Confirmation.builder().build());
            hit.setScene("CITY");
            hit.setAttrs(hitAttrs);
            hit.setCreationTime(createDate.getTime() / 1000);
            hit.setFirstHitTime(createDate.getTime() / 1000);
            hit.setLastModificationTime(new Date().getTime() / 1000);
            hitAttrs.setRoadName(zh.getRoadname());
            hitAttrs.getConfirmation().setStatus(Confirmation.ConfirmationStatus.CORRECT);
            hitAttrs.setFrameImageUrl(pictureUrl);
            hitAttrs.setSceneImage(pictureUrl);
            hitAttrs.setLimitHour(24);
            hitAttrs.setGpsLocation(gpsLocation);
            hitAttrs.setHandleCompany(zh.getCompanydesc());
            hitAttrs.setSection(zh.getPartofiddesc());
            hitAttrs.setHandlePerson(zh.getRecorder());
            hitAttrs.setStartHandleTime(processDate.getTime() / 1000);
            // 桥梁id
            hitAttrs.setDiseaseUid(zh.getBridgeid());
            hitAttrs.setDescriptionThird(zh.getBridgename() + " " + zh.getCompanydesc() + " " + componentDesc + " " + defeatType + " " + measure);
            hit.setId(hitId);
            hit.setType(type);
            hit.setNeedPushOut(false);
            generateHistory(zh, hit);
            return hit;
        } else {
            fetchHit.setLastModificationTime(new Date().getTime() / 1000);
            List<HistoryHit> list = fetchHit.getList();
            List<HitStatusEnum> collect = list.stream().map(history -> history.getAttrs().getStatus()).collect(Collectors.toList());
            HistoryHit historyHit = new HistoryHit();
            historyHit.setHitTime(createDate.getTime() / 1000);
            HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
            historyHitAttrs.setSceneImage(fetchHit.getAttrs().getSceneImage());
            historyHit.setAttrs(historyHitAttrs);
            historyHitAttrs.setGpsLocation(gpsLocation);
            if (Objects.equal(zh.getFootdisestatus(), "维修完工")) {
                if (collect.contains(HitStatusEnum.CONFIRMED)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.CONFIRMED);
                historyHitAttrs.setStatus(HitStatusEnum.CONFIRMED);
            } else if (Objects.equal(zh.getFootdisestatus(), "维修中")) {
                if (collect.contains(HitStatusEnum.COMPLETE)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.COMPLETE);
                historyHitAttrs.setStatus(HitStatusEnum.COMPLETE);
            } else if (Objects.equal(zh.getFootdisestatus(), "标段已提交")) {
                if (collect.contains(HitStatusEnum.DISPATCHED)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.DISPATCHED);
                historyHitAttrs.setStatus(HitStatusEnum.DISPATCHED);
            } else {
                if (collect.contains(HitStatusEnum.NEW)) {
                    return fetchHit;
                }
                fetchHit.getAttrs().setStatus(HitStatusEnum.NEW);
                historyHitAttrs.setStatus(HitStatusEnum.NEW);
            }
            list.add(0, historyHit);
            return fetchHit;
        }
    }

    private void generateHistory(BridgeDiseaseZH zh, Hit hit) {
        List<HistoryHit> historyHits;
        HistoryHit newHit = generateHistoryFromHit(zh, hit, NEW);
        HistoryHit dispatchedHit = generateHistoryFromHit(zh, hit, HitStatusEnum.DISPATCHED);
        HistoryHit completeHit = generateHistoryFromHit(zh, hit, HitStatusEnum.COMPLETE);
        HistoryHit confirmedHit = generateHistoryFromHit(zh, hit, HitStatusEnum.CONFIRMED);
        if (Objects.equal(zh.getFootdisestatus(), "维修完工")) {
            // 已复核
            hit.getAttrs().setStatus(HitStatusEnum.CONFIRMED);
            historyHits = Arrays.asList(confirmedHit, completeHit, dispatchedHit, newHit);
        } else if (Objects.equal(zh.getFootdisestatus(), "维修中")) {
            // 已处置
            hit.getAttrs().setStatus(HitStatusEnum.COMPLETE);
            historyHits = Arrays.asList(completeHit, dispatchedHit, newHit);
        } else if (Objects.equal(zh.getFootdisestatus(), "标段已提交")) {
            // 已派单
            hit.getAttrs().setStatus(HitStatusEnum.DISPATCHED);
            historyHits = Arrays.asList(dispatchedHit, newHit);
        } else {
            // 已发现
            hit.getAttrs().setStatus(NEW);
            historyHits = Arrays.asList(newHit);
        }
        hit.setList(historyHits);
    }

    private HistoryHit generateHistoryFromHit(BridgeDiseaseZH zh, Hit hit, HitStatusEnum status) {
        HistoryHit historyHit = new HistoryHit();
        HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
        historyHit.setAttrs(historyHitAttrs);
        // 不准
        historyHit.setHitTime(hit.getFirstHitTime());
        historyHitAttrs.setStatus(status);
        historyHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());
        // historyHitAttrs.setGpsLocation(GpsLocation.builder().latitude(Double.parseDouble()));
        return historyHit;
    }
}
